package com.topisv.tms.rest.qimen.qmservice;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXB;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.exception.ErrorCode;
import com.topisv.tms.exception.PaiUException;
import com.topisv.tms.repository.EdiGoodsSkuDao;
import com.topisv.tms.repository.EdiOrderItemDao;
import com.topisv.tms.rest.qimen.qmentity.request.orderoutofstock.Item;
import com.topisv.tms.rest.qimen.qmentity.request.orderoutofstock.OrderOutofstock;
import com.topisv.tms.rest.qimen.qmsend.QmRequestSender;
import com.topisv.tms.rest.qimen.request.RequestMethodType;
import com.topisv.tms.rest.qimen.request.RequestParams;
import com.topisv.tms.utils.InitProperties;
import com.topisv.tms.utils.TimeUtil;
import com.topisv.tms.utils.rule.TMSTransportRule;

/**
 * 销售出库单缺货通知接口 create by gaohuan 2015-05-09
 * */
@Component
@Transactional(readOnly =  false)
public class OrderOutofstockService  extends QmRequestSender<EdiOrder> {
	
	private static final Logger logger = LoggerFactory.getLogger(OrderOutofstockService.class);
	@Autowired
	private EdiOrderItemDao ediOrderItemDao;
	
	@Autowired
	private EdiGoodsSkuDao ediGoodsSkuDao;
	
	
	@Transactional(readOnly = false,rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
	public String execute(EdiOrder ediOrder,RequestParams sendParams)throws PaiUException{
//		EdiOrder ediOrder = (EdiOrder)params.get("ediOrder");
		if(ediOrder==null){
			logger.info("ediOrder 为空");
//			super.setAutoSend(false);
			return null;
		}
		sendParams.getParams().put("customerId", ediOrder.getCustomerCode());
		String sendXml = "";
		try{
			
			OrderOutofstock oo = new OrderOutofstock();
			oo.setWarehouseCode(ediOrder.getWarehouseCode());
			oo.setDeliveryOrderCode(ediOrder.getCustomerOrdeno());
			oo.setDeliveryOrderId(ediOrder.getOrdeno());
			oo.setCreateTime(TimeUtil.getNowDateString());
			oo.setOutBizCode(TMSTransportRule.generateByPreFixAndSuffix("QM","OO"));
			List<Item> items = new ArrayList<Item>();
			Item item = null;
			logger.info("报缺接口查询订单明细");
			List<EdiOrderItem> eoiList = ediOrder.getOrderItems();
			if(eoiList==null){
				eoiList = ediOrderItemDao.findByOrdeno(ediOrder.getOrdeno());
			}
			for(EdiOrderItem eoi:eoiList) {
				item = new Item();
				EdiGoodsSku ediSku = ediGoodsSkuDao.findByCustomerCodeAndSku(ediOrder.getCustomerCode(), eoi.getSku());
				
				item.setItemCode(ediSku.getBar());
				item.setItemId(eoi.getId()+"");
				item.setPlanQty((eoi.getQty()==null)?"":(eoi.getQty()+""));
				item.setLackQty(item.getPlanQty());
				item.setReason("系统报缺");
				
				items.add(item);
			}
			oo.setItems(items);
			StringWriter writer = new StringWriter();
			JAXB.marshal(oo,writer);
			sendXml = writer.toString();
			
		}catch(PaiUException e){
        	throw new PaiUException(ErrorCode.DATA_ERROR);
		}catch(Exception e){
        	throw new PaiUException(ErrorCode.DATA_ERROR);
		}
		return sendXml;
	}
	
	@Override
	public RequestParams setupParam(RequestParams rp) {
		
		Map<String,String> params = new HashMap<String,String>();
		params.put("method", RequestMethodType.ITEMLACK_REPORT.getType());
		params.put("format", InitProperties.get("qm.format"));
		params.put("app_key", InitProperties.get("regAppKey"));
		params.put("v", InitProperties.get("qm.v"));
		params.put("sign_method", InitProperties.get("qm.sign_method"));
		params.put("customerId", "");//暂未定
		rp.setSecret(InitProperties.get("regAppSecret"));
		rp.setParams(params);
		return rp;
	}
}
